from selenium import webdriver
import time
url = 'http://www.126.com/'

driver = webdriver.Chrome()

# 访问126邮箱
driver.get(url)
time.sleep(5)
# # 直接通过id进入
# driver.switch_to.frame('x-URS-iframe')
# # 先定位到元素再进入
# el_frame = driver.find_element_by_id('x-URS-iframe')
# driver.switch_to.frame(el_frame)
# 无id，无name,先定位iframe元素
iframe = driver.find_elements_by_tag_name("iframe")[0]
driver.switch_to.frame(iframe)
try:
    # 定位并输入账号
    el_user = driver.find_element_by_name('email')
    el_user.send_keys('itcast_soft_test')
    # 定位并输入密码
    el_pwd = driver.find_element_by_name('password')
    el_pwd.send_keys('1qaz2wsx#EDC')
    # 定位并点击登录
    el_sub = driver.find_element_by_id('dologin')
    el_sub.click()
    time.sleep(5)
except:
    print ('页面上没有账号密码输入框')

driver.quit()